<script setup>
import { EditPen, Lock, Message } from '@element-plus/icons-vue'
import router from '@/router'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { post } from '@/net'

const formRef = ref()
const isEmailValid = ref(false)
const coldTime = ref(0)
const active = ref(0)

const form = reactive({
  password: '',
  password_repeat: '',
  email: '',
  code: ''
})

// y
const validatePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else {
    if (form.password !== value) {
      callback(new Error('两次密码不一致'))
    }
    callback()
  }
}

const rules = {
  password: [
    { required: true, message: '请输入秘密', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度必须在6-16个字符之间', trigger: ['blur', 'change'] }
  ],
  password_repeat: [
    { validator: validatePassword, trigger: ['blur', 'change'] },
    { min: 6, max: 16, message: '密码长度必须在6-16个字符之间', trigger: ['blur', 'change'] }
  ],
  email: [
    { required: true, message: '请输入电子邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入合法的电子邮箱地址', trigger: ['blur', 'change'] }
  ],
  code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
}

const onValidate = (prop, isValid) => {
  if (prop === 'email') isEmailValid.value = isValid
}

const validEmailAndCode = () => {
  formRef.value.validate((isValid) => {
    if (!isValid) ElMessage.warning('请完整填写注册表单内容～')
    else {
      post(
        '/api/auth/start-reset',
        {
          email: form.email,
          code: form.code
        },
        (message) => {
          active.value++
          ElMessage.success(message)
        }
      )
    }
  })
}
const getCode = () => {
  coldTime.value = 60
  post(
    '/api/auth/valid-reset-email',
    {
      email: form.email
    },
    (message) => {
      ElMessage.success(message)
      // 定时器
      setInterval(() => coldTime.value--, 1000)
    },
    (message) => {
      ElMessage.warning(message)
      coldTime.value = 0
    }
  )
}

const forget = () => {
  formRef.value.validate((isValid) => {
    if (!isValid) ElMessage.warning('请完整填写注册表单内容～')
    else {
      post(
        '/api/auth/do-reset',
        {
          password: form.password
        },
        (message) => {
          ElMessage.success(message)
          router.push('/')
        }
      )
    }
  })
}
</script>

<template>
  <div>
    <div style="margin: 30px 20px">
      <el-steps :space="200" :active="active" finish-status="success" align-center>
        <el-step title="验证邮箱"></el-step>
        <el-step title="重置密码"></el-step>
      </el-steps>
    </div>

    <div style="text-align: center; margin: 0 20px">
      <div style="margin-top: 100px" v-if="active === 0">
        <div style="font-size: 25px; font-weight: bold">验证邮箱</div>
        <div style="font-size: 14px; color: rgb(128, 128, 128)">
          请输入需要重置密码的电子邮件地址
        </div>
      </div>

      <div style="margin-top: 100px" v-if="active === 1">
        <div style="font-size: 25px; font-weight: bold">重置密码</div>
        <div style="font-size: 14px; color: rgb(128, 128, 128)">
          请填写您的新密码，务必牢记，防止丢失
        </div>
      </div>

      <div style="margin-top: 50px">
        <el-form
          v-if="active === 0"
          :model="form"
          :rules="rules"
          @validate="onValidate"
          ref="formRef"
        >
          <el-form-item prop="email">
            <el-input v-model="form.email" type="text" placeholder="电子邮箱地址">
              <template #prefix>
                <el-icon><Message /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="code"
            ><el-row :gutter="10" style="width: 100%">
              <el-col :span="17">
                <el-input
                  v-model="form.code"
                  type="text"
                  :maxlength="6"
                  placeholder="电子邮箱地址验证码"
                >
                  <template #prefix>
                    <el-icon><EditPen /></el-icon>
                  </template>
                </el-input>
              </el-col>
              <el-col :span="7">
                <el-button
                  type="success"
                  plain
                  :disabled="!isEmailValid || coldTime > 0"
                  @click="getCode"
                >
                  {{ coldTime > 0 ? '请稍后' + coldTime + ' 秒' : '获取验证码' }}</el-button
                >
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>

        <el-form
          :model="form"
          :rules="rules"
          @validate="onValidate"
          ref="formRef"
          v-if="active === 1"
        >
          <el-form-item prop="password">
            <el-input v-model="form.password" type="password" :maxlength="16" placeholder="新密码">
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password_repeat">
            <el-input
              v-model="form.password_repeat"
              type="password"
              :maxlength="16"
              placeholder="请再次确任新密码"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>

      <div style="margin-top: 50px">
        <div>
          <el-button
            v-if="active === 0"
            style="width: 270px"
            type="warning"
            :disabled="!isEmailValid"
            plain
            @click="validEmailAndCode"
            >下一步</el-button
          >

          <el-button
            v-if="active === 1"
            style="width: 270px"
            type="warning"
            :disabled="!isEmailValid"
            plain
            @click="forget"
            >重置</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped></style>
